<?php

Class Category extends CI_Model {

    function category_dropdown() {
        //$nor=$this->config->item('number_of_rows');
        $query = $this->db->query("select * from question_category");

        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return false;
        }
    }

// get alll category
    function category_list() {
        // Tâm
        //$institute_id = $this->session->userdata('institute_id');
        //$this->db->where('institute_id', $institute_id);
        $this->db->select('cid, category_name');
        $this->db->from('question_category');
        //$this->db->limit($this->config->item('number_of_rows'), $limit);
        $this->db->order_by("cid", "desc");
        $query = $this->db->get();

        if ($query->num_rows() >= 1) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    function remove_category($cid) {
        $institute_id = $this->session->userdata('institute_id');
        $this->db->where('institute_id', $institute_id);

        if ($this->db->delete('question_category', array('cid' => $cid))) {
            return true;
        } else {
            return false;
        }
    }
    
    // Tâm
    function insert($new) {
        if ($this->db->insert('question_category', $new)) {
            return "Đã thêm!";
        } else {
            return "Unable to add";
        }
    }
    
    function delete($id) {
        
        $q1 = $this->db->get_where('qbank', array('cid' => $id));
        $questions = $q1->result_array();
        foreach ($questions as $eachQ) {
            $this->db->delete('q_options', array('qid' => $eachQ['qid']));
        }
        $this->db->delete('qbank', array('cid' => $id));
        $this->db->delete('question_category', array('cid' => $id));
        return "Đã xóa!";
        
    }

    // insert category
    function insert_category() {
        $institute_id = $this->session->userdata('institute_id');
        $insert_category = array(
            'category_name' => $this->input->post('categoryname'),
            'institute_id' => $institute_id
        );

        if ($this->db->insert('question_category', $insert_category)) {
            return "Category added successfully";
        } else {
            return "Unable to add category";
        }
    }

    // get particular category detail		
    function get_category($cid) {
        $institute_id = $this->session->userdata('institute_id');
        $query = $this->db->get_where('question_category', array('cid' => $cid, 'institute_id' => $institute_id));
        return $query->row_array();
    }

    // update category detail
    function update_category($cid) {

        $categoryname = $_POST['categoryname'];
        $category_detail = array(
            'category_name' => $categoryname,
        );
        $institute_id = $this->session->userdata('institute_id');
        $this->db->where('institute_id', $institute_id);

        $this->db->where('cid', $cid);
        $this->db->update('question_category', $category_detail);
        return "Category updated";
    }

}
?>

